package com.xbj.cloud.test.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author: 观雾
 * @create: 2024-09-17
 * @description:
 **/
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //没有返回值
        runAsyncDemo();
        //有返回值
        supplyAsyncDemo1();
        //不阻塞
        supplyAsyncDemo2();

        supplyAsyncDemo3();
    }

    public static void runAsyncDemo() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            try {
                System.out.println("当前线程名称" + Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, threadPool);
        System.out.println("返回值:"+completableFuture.get());

        //关闭线程池
        threadPool.shutdown();
    }

    public static String supplyAsyncDemo1() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程名称:" + Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello,supplyAsync";
        }, threadPool);
        System.out.println("返回值:"+completableFuture.get());
        //关闭线程池
        threadPool.shutdown();
        return completableFuture.get();
    }
    public static void supplyAsyncDemo2(){
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+"come in");
            int result = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("1秒后出结果:"+result);
            if(result>2){
                int i = 10 / 0;
            }
            return result;
        },threadPool).whenComplete((v,e)->{
            System.out.println("whenComplete come in ");
            if(e == null){
                System.out.println("计算完成返回结果:"+v);
            }
        }).exceptionally(e->{
            System.out.println(e.getCause()+"\t"+e.getCause().getMessage());
            return null ;
        });
        System.out.println("主线程先去忙其他任务");
        //关闭线程池
        threadPool.shutdown();
    }

    public static void supplyAsyncDemo3(){
        System.out.println("=======");
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "abc");
        System.out.println("supplyAsyncDemo3结果:"+ completableFuture.join());
    }
}
